package com.bdjk.bdmw.InfoGath.business.beianbei.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.bdjk.bdmw.InfoGath.business.beianbei.dao.BeiAnBeiPositionDAO;
import com.bdjk.bdmw.InfoGath.business.beianbei.model.BeiAnBeiPositionModel;
import com.bdjk.bdmw.InfoGath.business.beianbei.vo.BeiAnBeiPositionVO;
import com.bdjk.bdmw.InfoGath.business.heremi.model.HereMiPositionModel;
import com.bdjk.bdmw.InfoGath.business.heremi.vo.HereMiPositionVO;
import com.bdjk.bdmw.InfoGath.common.dao.impl.JdbcDAOImpl;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;

public class BeiAnBeiPositionDAOImpl extends JdbcDAOImpl implements BeiAnBeiPositionDAO {

	@Override
	public int savePosition(BeiAnBeiPositionModel model) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		sql.append("insert into t_position (ter_ident,send_time,longitude,latitude,altitude,speed,direction)values(?,?,?,?,?,?,?)");
		List<Object> params = new ArrayList<Object>();
		params.add(model.getTerIdent());
		params.add(model.getSendTime());
		params.add(model.getLongitude());
		params.add(model.getLatitude());
        params.add(model.getAltitude());
		params.add(model.getSpeed());
		params.add(model.getDirection());
		int positionId=0;
		try {
			positionId=executeSQL(sql.toString(), params);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return positionId;
	}

	@Override
	public int saveExtension(BeiAnBeiPositionModel model, int id) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		sql.append("insert into t_position_extension (position_id,ter_ident,sendTime,baseInfo,wifiInfo,pro_code,is_echoLocation,satellite_num,gsm_sign_strength,electricity,step_num,roll_num,ter_status,bab_comm_type)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		params.add(model.getTerIdent());
		params.add(model.getSendTime());
		params.add(model.getBaseInfo());
		params.add(model.getWifiInfo());
		params.add(model.getProCode());
		params.add(model.getIsEchoLocation());
		params.add(model.getSatelliteNum());
        params.add(model.getGsmSignStrength());
        
		params.add(model.getElectricity());
		params.add(model.getStepNum());
		params.add(model.getRollNum());
		params.add(model.getTerStatus());
		params.add(model.getCommType());
		try {
			executeSQL(sql.toString(), params);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<BeiAnBeiPositionModel> queryPosition(List<Object> paramList)
			throws BusinessException {
		// TODO Auto-generated method stub
		String sql=new String("SELECT * FROM t_position WHERE UNIX_TIMESTAMP( send_time ) BETWEEN UNIX_TIMESTAMP( ?) AND UNIX_TIMESTAMP( ? ) AND ter_ident=?");
		List<Object> sqlrs=executeResultSQL(sql.toString(),paramList);
		List<BeiAnBeiPositionModel> result=new ArrayList<BeiAnBeiPositionModel>();
		for(int i=0;i<sqlrs.size();i++){
			BeiAnBeiPositionVO vo=new BeiAnBeiPositionVO();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			int col=0;
			vo.setId(rs.get(col).toString());col++;
			vo.setTerIdent(rs.get(col).toString());col++;
			vo.setSendTime(rs.get(col).toString());col++;
			vo.setLongitude(rs.get(col).toString());col++;
			vo.setLatitude(rs.get(col).toString());col++;
			vo.setAltitude(rs.get(col).toString());col++;
			vo.setSpeed(rs.get(col).toString());col++;
			vo.setDirection(rs.get(col).toString());col++;
			
		
			//vo.setUpdateTime((Date)rs.get(col));col++;
			BeiAnBeiPositionModel model=new BeiAnBeiPositionModel();
			try {
				model = vo.vo2Model();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.add(model);
		}
		
		return result;
	}

	@Override
	public BeiAnBeiPositionModel queryCurrentPosition(List<Object> paramList)
			throws BusinessException {
		// TODO Auto-generated method stub
		String sql="SELECT * FROM (SELECT *  FROM t_position WHERE ter_ident=?)t ORDER BY send_time DESC LIMIT 0,1";
		
		BeiAnBeiPositionModel model=null;
		List<BeiAnBeiPositionModel> result=query(sql,paramList);
		if(result.size()>0){
			model=result.get(0);
		}
		return model;
	}
	public List<BeiAnBeiPositionModel> query(String sql,List<Object> paramList) throws BusinessException {
		List<BeiAnBeiPositionModel> result=new ArrayList<BeiAnBeiPositionModel>();
		List<Object> sqlrs=executeResultSQL(sql.toString(),paramList);
		for(int i=0;i<sqlrs.size();i++){
			BeiAnBeiPositionVO vo=new BeiAnBeiPositionVO();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			int col=0;
			vo.setId(rs.get(col).toString());col++;
			vo.setTerIdent(rs.get(col).toString());col++;
			vo.setSendTime(rs.get(col).toString());col++;
			vo.setLongitude(rs.get(col).toString());col++;
			vo.setLatitude(rs.get(col).toString());col++;
			vo.setAltitude(rs.get(col).toString());col++;
			vo.setSpeed(rs.get(col).toString());col++;
			vo.setDirection(rs.get(col).toString());col++;

			BeiAnBeiPositionModel model=new BeiAnBeiPositionModel();
			try {
				model = vo.vo2Model();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.add(model);
		}
		return result;
	}
}
